package com.weimob.mcloud.wechatwork.customer.convertor.bos;

import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphereVid;
import com.weimob.mcloud.wechatwork.core.enums.privateSphere.PrivateSphereVidTypeEnum;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PrivateSphereDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PrivateSphereVidDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.enums.VidTypeEnum;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/12/8 4:31 下午
 */
public class PrivateSphereCovert {

    public static PrivateSphere toPrivateSphere(PrivateSphereDTO privateSphereDTO) {
        if (Objects.isNull(privateSphereDTO)) {
            return null;
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setPrivateSphereId(privateSphereDTO.getPrivateSphereId());
        privateSphere.setPrivateSphereName(privateSphereDTO.getPrivateSphereName());
        privateSphere.setProductId(privateSphereDTO.getProductId());
        privateSphere.setProductInstanceId(privateSphereDTO.getProductInstanceId());
        privateSphere.setBosId(privateSphereDTO.getBosId());
        privateSphere.setPid(privateSphereDTO.getPid());
        privateSphere.setCorpId(privateSphereDTO.getCorpId());
        privateSphere.setStatus(privateSphereDTO.getStatus());
        privateSphere.setSuiteId(privateSphereDTO.getSuiteId());
        privateSphere.setAgentId(privateSphereDTO.getAgentId());
        privateSphere.setExtInfo(privateSphereDTO.getExtInfo());

        List<PrivateSphereVid> privateSphereVids = new ArrayList<>();
        List<PrivateSphereVidDTO> privateSphereDTOVids = privateSphereDTO.getVidList();
        if (CollectionUtils.isNotEmpty(privateSphereDTOVids)) {
            for (PrivateSphereVidDTO vidDTO : privateSphereDTOVids) {
                PrivateSphereVid privateSphereVid = toPrivateSphereVid(vidDTO);
                if (Objects.nonNull(privateSphereVid)) {
                    privateSphereVids.add(privateSphereVid);
                }
            }

            privateSphere.setVids(privateSphereVids);
        }


        return privateSphere;
    }

    public static PrivateSphereVid toPrivateSphereVid(PrivateSphereVidDTO privateSphereVidDTO) {

        if (Objects.isNull(privateSphereVidDTO)) {
            return null;
        }

        PrivateSphereVid privateSphereVid = new PrivateSphereVid();
        privateSphereVid.setId(privateSphereVidDTO.getId());
        privateSphereVid.setVid(privateSphereVidDTO.getVid());
        privateSphereVid.setRootVid(privateSphereVidDTO.getRootVid());
        privateSphereVid.setParentVid(privateSphereVidDTO.getParentVid());
        privateSphereVid.setStoreId(privateSphereVidDTO.getStoreId());
        privateSphereVid.setPrivateSphereId(privateSphereVidDTO.getPrivateSphereId());
        privateSphereVid.setCreateTime(privateSphereVidDTO.getCreateTime());
        privateSphereVid.setUpdateTime(privateSphereVidDTO.getUpdateTime());
        privateSphereVid.setDeleted(privateSphereVidDTO.getDeleted());

        VidTypeEnum vidType = privateSphereVidDTO.getVidType();
        if (Objects.nonNull(vidType)) {
            PrivateSphereVidTypeEnum vidTypeEnum = PrivateSphereVidTypeEnum.of(vidType.getCode());
            privateSphereVid.setVidType(vidTypeEnum);
        }


        return privateSphereVid;
    }

    public static PrivateSphereDTO privateSphereDTO(PrivateSphere privateSphere) {

        if (Objects.isNull(privateSphere)) {
            return null;
        }

        PrivateSphereDTO privateSphereDTO = new PrivateSphereDTO();
        privateSphereDTO.setPrivateSphereId(privateSphere.getPrivateSphereId());
        privateSphereDTO.setPrivateSphereName(privateSphere.getPrivateSphereName());
        privateSphereDTO.setProductId(privateSphere.getProductId());
        privateSphereDTO.setProductInstanceId(privateSphere.getProductInstanceId());
        privateSphereDTO.setBosId(privateSphere.getBosId());
        privateSphereDTO.setPid(privateSphere.getPid());
        privateSphereDTO.setCorpId(privateSphere.getCorpId());
        privateSphereDTO.setStatus(privateSphere.getStatus());
        privateSphereDTO.setSuiteId(privateSphere.getSuiteId());
        privateSphereDTO.setAgentId(privateSphere.getAgentId());
        privateSphereDTO.setExtInfo(privateSphere.getExtInfo());

        List<PrivateSphereVidDTO> privateSphereVidDTOS = new ArrayList<>();
        List<PrivateSphereVid> vids = privateSphere.getVids();
        if (CollectionUtils.isNotEmpty(vids)) {
            for (PrivateSphereVid privateSphereVid : vids) {
                PrivateSphereVidDTO vidDTO = toPrivateSphereVidDTO(privateSphereVid);
                if (Objects.nonNull(vidDTO)) {
                    privateSphereVidDTOS.add(vidDTO);
                }
            }

            privateSphereDTO.setVidList(privateSphereVidDTOS);


        }
        return privateSphereDTO;
    }

    public static PrivateSphereVidDTO toPrivateSphereVidDTO(PrivateSphereVid privateSphereVid) {

        if (Objects.isNull(privateSphereVid)) {
            return null;
        }


        PrivateSphereVidDTO privateSphereVidDTO = new PrivateSphereVidDTO();
        privateSphereVidDTO.setId(privateSphereVid.getId());
        privateSphereVidDTO.setVid(privateSphereVid.getVid());
        privateSphereVidDTO.setRootVid(privateSphereVid.getRootVid());
        privateSphereVidDTO.setParentVid(privateSphereVid.getParentVid());
        privateSphereVidDTO.setStoreId(privateSphereVid.getStoreId());
        privateSphereVidDTO.setPrivateSphereId(privateSphereVid.getPrivateSphereId());
        privateSphereVidDTO.setCreateTime(privateSphereVid.getCreateTime());
        privateSphereVidDTO.setUpdateTime(privateSphereVid.getUpdateTime());
        privateSphereVidDTO.setDeleted(privateSphereVid.getDeleted());

        PrivateSphereVidTypeEnum vidType = privateSphereVid.getVidType();
        if (Objects.nonNull(vidType)) {
            VidTypeEnum of = VidTypeEnum.of(vidType.getCode());
            privateSphereVidDTO.setVidType(of);
        }


        return privateSphereVidDTO;
    }
}
